package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CalculationRebatesDTO;
import org.jsola.hr.dto.SaleRebatesRecordAddDTO;
import org.jsola.hr.dto.SaleRebatesRecordUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.query.SaleRebatesRecordQuery;
import org.jsola.hr.service.ISaleRebatesRecordService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 返费记录
 *
 * @author zhr
 */
@RestController("hrSaleRebatesRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/saleRebatesRecords")
public class SaleRebatesRecordController implements ISaleRebatesRecordControllerAPI {

    @Autowired
    private ISaleRebatesRecordService saleRebatesRecordService;

    @Override
    @PostMapping
    public Result<SaleRebatesRecordVO> save(@Valid @RequestBody SaleRebatesRecordAddDTO saleRebatesRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增返费记录权限校验

        return Result.success(
                saleRebatesRecordService.save(saleRebatesRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SaleRebatesRecordUpdateDTO saleRebatesRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改返费记录权限校验

        int count = saleRebatesRecordService.update(saleRebatesRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{saleRebatesRecordId}")
    public Result<Integer> delete(@PathVariable String saleRebatesRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除返费记录权限校验

        int count = saleRebatesRecordService.deleteByIds(tokenUser, saleRebatesRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] saleRebatesRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除返费记录权限校验

        int count = saleRebatesRecordService.deleteByIds(tokenUser, saleRebatesRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{saleRebatesRecordId}")
    public Result<SaleRebatesRecordVO> findById(@PathVariable String saleRebatesRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看返费记录权限校验

        return Result.success(
                saleRebatesRecordService.selectById(saleRebatesRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SaleRebatesRecordListVO>> listAll(@Valid SaleRebatesRecordQuery saleRebatesRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看返费记录权限校验

        return Result.success(
                saleRebatesRecordService.select(saleRebatesRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SaleRebatesRecordListVO>> page(@Valid SaleRebatesRecordQuery saleRebatesRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.selectPage(saleRebatesRecordQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/calculationRebates")
    public Result<CalculationRebatesVO> calculationRebates(@RequestBody CalculationRebatesDTO calculationRebatesDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.calculationRebates(calculationRebatesDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/myCommission")
    public Result<MyCommissionVO> myCommission(@Valid SaleRebatesRecordQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.myCommission(query, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectCompanyByUser")
    public Result<List<CompanyDO>> selectCompanyByUser() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.selectCompanyByUser(tokenUser)
        );
    }

    @Override
    @GetMapping("/rebateOverviewList")
    public Result<Page<SaleRebatesRecordListVO>> rebateOverviewList(@Valid SaleRebatesRecordQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.rebateOverviewList(query, tokenUser)
        );
    }

    @Override
    @GetMapping("/rebateOverviewGroup")
    public Result<RebateOverviewGroupVO> rebateOverviewGroup(@Valid SaleRebatesRecordQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.rebateOverviewGroup(query, tokenUser)
        );
    }

    @Override
    @GetMapping("/rebateOverviewMonth")
    public Result<RebateOverviewMonthVO> rebateOverviewMonth(@Valid SaleRebatesRecordQuery query) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                saleRebatesRecordService.rebateOverviewMonth(query, tokenUser)
        );
    }
}

